{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###   编码 : 将人类的语言符号转化为二进制 \n",
    "\n",
    "#存储单位  \n",
    "#【最小存储单位】bit位 : 0/1     \n",
    "#【最常用单位】byte字节 :  0000 0000 = 256  \n",
    "# 1B(byte) = 8bit\n",
    "# 1KB = 1024 B ； 1MB = 1024 KB ;   1GB = 1024 MB\n",
    "# 运营商 100M带宽 = 100Mbit/s = 12.5 M KB/s = 12.5 MB/s\n",
    "\n",
    "# 编码表【计算机世界的字典】 : \n",
    "# ASCII编码规定欧美人常用符号的二进制表示法：字母、数字再加上其他常用符号\n",
    "# GB2312标准用两个byte（65535bit）表示汉字\n",
    "# >>> Unicode（万国码）诞生 : 将世界上所有的符号都纳入其中。每个符号都有各自编码，容纳100多万个符号，所有语言都可以互通，一个网页上也可以显示多国语言。\n",
    "# >>> UTF-8（保存和传输Unicode的手段） : 为了节省bit，针对Unicode的可变长度字符编码，它可以使用1~4个字节表示一个符号，根据不同的符号而变化字节长度，而当字符在ASCII码的范围时，就用一个字节表示，所以UTF-8还可以兼容ASCII编码。\n",
    "\n",
    "# 编码 str.encode()  字符 → 字节\n",
    "# 本质：把str（字符串）类型的数据，利用不同的编码表，转换成bytes（字节）类型的数据\n",
    "UTF8编码表内的我爱你 = '我爱你'.encode('utf-8')\n",
    "print(UTF8编码表内的我爱你)    # 编码后的 b' 代表【字节类型】的数据\n",
    "print(type(b'\\xe6\\x88\\x91\\xe7\\x88\\xb1\\xe4\\xbd\\xa0'))   # \\x 代表字节分隔符\n",
    "\n",
    "# 解码 byte.decode()   字节 → 字符\n",
    "words = b'\\xe6\\x88\\x91\\xe7\\x88\\xb1\\xe4\\xbd\\xa0'.decode('utf-8')\n",
    "print(words)   # UTF-8编码的字节就一定要用UTF-8的规则解码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "b'\\xe6\\x9d\\xa8\\xe8\\x8b\\xb1\\xe6\\x9d\\xb0'\nb'\\xd1\\xee\\xd3\\xa2\\xbd\\xdc'\n杨英杰\n"
     ]
    }
   ],
   "source": [
    "print('杨英杰'.encode('utf-8'))\n",
    "print('杨英杰'.encode('gbk'))\n",
    "\n",
    "print(b'\\xd1\\xee\\xd3\\xa2\\xbd\\xdc'.decode('gbk'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###   文件读写 open()函数返回一个可迭代的文件对象\n",
    "\n",
    "#  写入文件   开》写》关 \n",
    "file1 = open(r'路径\\abc.txt','w',encoding='utf-8') \n",
    "file1.write('张三丰\\n')   \n",
    "file1.write('宋老板\\n')\n",
    "file1.close()   #'w'写入模式会给你暴力清空掉文件，然后再写入。\n",
    "\n",
    "#  读取文件   开》读》关   \n",
    "file2 = open(r'路径','r',encoding='utf-8')   # encoding 不是encode\n",
    "# 相对/绝对路径 r为地址类型避免与转义字符\\混淆 ,'r' 为读取 ，返回的数据采用何种编码读取\n",
    "filecontent = file2.read()\n",
    "file2.close()  #关\n",
    "print(filecontent)\n",
    "\n",
    "#  追加文件   开》写》关 \n",
    "file1 = open(r'路径','a',encoding='utf-8') \n",
    "# filecontent = file1.write('赵敏\\n')    错误写法 ： 返回个数\n",
    "file1.write('周芷若\\n')  \n",
    "file1.write('赵敏\\n') \n",
    "file1.close()   #"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 另一种写法 with语句\n",
    "# 定义和控制代码块执行前的准备工作及执行后的收尾工作?\n",
    "import csv\n",
    "\n",
    "with open('abc.txt','a') as file1:  # file1 = open('abc.txt','a')\n",
    "#with open('文件地址','读写模式') as 文档指针:      # 将指针指向文档地址\n",
    "    file1.write('张无忌')       # 文档指针.操作('内容')      ① \n",
    "    f_csv = csv.writer(file1)   # 实例化对象 = 模块.类(文档指针)     ②:将指针指向f_csv\n",
    "    f_csv.writerow('[a,b,v]')   # 对象.所需函数('内容')     ② \n",
    "    f_csv.seek(0)               # 将文件指针移动到文件开头\n",
    "    f_csv.readrow()\n",
    "    print(file1)\n",
    "    print(f_csv)\n",
    "    #格式：对文件的操作要缩进\n",
    "    #格式：主动关闭文件句柄,无需用close()关闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "163Music.py has 123 lines\n"
     ]
    }
   ],
   "source": [
    "# 读取某个py文件的代码行数\n",
    "file_name = '163Music.py'\n",
    "try:\n",
    "    with open(file_name,encoding='utf-8') as f:\n",
    "        data = f.readlines()\n",
    "except FileNotFoundError:\n",
    "    print(file_name+' does not exist')\n",
    "lens = len(data)\n",
    "print(file_name+' has '+str(lens)+' lines')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "293\n"
     ]
    }
   ],
   "source": [
    "# 计算当前工作目录下所有py文件的行数\n",
    "import os\n",
    "\n",
    "def countLines(fname):\n",
    "    'count lines in fname'\n",
    "    try:\n",
    "        with open(fname,encoding='utf-8') as f:\n",
    "            data = f.readlines()\n",
    "    except:\n",
    "        print(fname+' does not exist')\n",
    "    lens = len(data)\n",
    "    return lens\n",
    "\n",
    "path = '../Learning pro'    # 返回上一层目录,进入Learning pro文件\n",
    "lensSum = 0\n",
    "for f in os.listdir(path):\n",
    "    if f.endswith('.py'):\n",
    "        file_path = os.path.join(path,f)\n",
    "        lensSum += countLines(file_path)\n",
    "print(lensSum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "{'joker': 'joe', 'nacy': 'timi'}\n"
     ]
    }
   ],
   "source": [
    "# 存储为json文件\n",
    "import json\n",
    "\n",
    "names = {'joker':'joe','nacy':'timi'}\n",
    "\n",
    "with open('names.json','w') as file_obj:\n",
    "    json.dump(names,file_obj)       #将names存入file_obj\n",
    "\n",
    "with open('names.json') as file_obj:\n",
    "    names = json.load(file_obj)     #从file_obj读取names\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写论文时遇到的问题\n",
    "import csv\n",
    "import json\n",
    "\n",
    "A = [0.0000000062,0.0000000070,0.0000000083]    ##  单位 s\n",
    "B = [0,157870,0]   ##  单位 W\n",
    "C = []\n",
    "D = []\n",
    "i = 0 \n",
    "while i < 1000 :     ## 脉冲个数\n",
    "    C.append(A[0] + 0.001*i)    #   脉冲周期为1ms   -5为零      20   -    70一直升\n",
    "    C.append(A[1] + 0.001*i)\n",
    "    C.append(A[2] + 0.001*i)\n",
    "    D.append(B[0]/8)\n",
    "    D.append(B[1]/8)\n",
    "    D.append(B[2]/8)\n",
    "    i = i+1\n",
    "print(C)\n",
    "print(D)\n",
    "routine = r'C:\\Users\\GilesYang\\Desktop\\Wipda_Asia\\DSRD_1_做了板子_皮\\DSRD-PSpiceFiles\\SCHEMATIC1\\计算书2.csv'\n",
    "c = json.dumps(C)\n",
    "d = json.dumps(D)\n",
    "\n",
    "with open(routine,'w',newline = '',encoding = 'gbk') as csv_file:\n",
    "    csv_file.write(c)\n",
    "    csv_file.write('\\n')\n",
    "    csv_file.write(d)\n",
    "    #格式：对文件的操作要缩进\n",
    "    #格式：无需用close()关闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面注释掉的代码，皆为检验代码（验证每一步的思路和代码是否达到目标，可解除注释后运行）。\n",
    "\n",
    "x = open(r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\15~17关：第5个小目标：用Python给朋友发个邮件\\winner.txt','r',encoding='utf-8') \n",
    "file_lines = x.readlines() \n",
    "x.close()\n",
    "dict_scores = {}\n",
    "list_scores = []\n",
    "final_scroes = []\n",
    "\n",
    "print(file_lines) \n",
    "\n",
    "# # 经过测试，发现'\\n'的长度是1。所以，名字是“第0位-倒数第5位”，分数是“倒数第4位-倒数第二位”。\n",
    "# # 再根据“左取右不取”，可知：name-[:-4],score-[-4:-1]\n",
    "for i in file_lines: \n",
    "    name = i[:-4]        # 取出名字\n",
    "    score = int(i[-4:-1])      # 取出成绩\n",
    "    dict_scores[score] = name\n",
    "    list_scores.append(score)\n",
    "    \n",
    "list_scores.sort(reverse=True)  #顺序排序\n",
    "\n",
    "for i in list_scores:\n",
    "    result = dict_scores[i]+':'+str(i)+'\\n'\n",
    "    final_scroes.append(result)\n",
    "\n",
    "print(final_scroes)\n",
    "\n",
    "y = open(r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\15~17关：第5个小目标：用Python给朋友发个邮件\\winner_new.txt','w',encoding='utf-8')\n",
    "y.writelines(final_scroes)  #用lines写入列表\n",
    "# y.write(final_scroes)  #无法写入字典\n",
    "y.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典内排序\n",
    "dict_scores = {}\n",
    "dict_scores2 = {}\n",
    "list_scores = []\n",
    "file_lines = ['罗恩102\\n', '哈利383\\n', '赫敏570\\n', '马尔福275\\n']\n",
    "\n",
    "for i in file_lines: \n",
    "    name = i[:-4]        # 取出名字\n",
    "    score = int(i[-4:-1])      # 取出成绩\n",
    "    dict_scores[score] = name\n",
    "    list_scores.append(score)\n",
    "    \n",
    "list_scores.sort(reverse=True)\n",
    "\n",
    "print(dict_scores)\n",
    "\n",
    "for i in range(len(dict_scores)):\n",
    "    dict_scores2[list_scores[i]] = dict_scores[list_scores[i]]\n",
    "    \n",
    "print(dict_scores2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实现图片复制粘贴\n",
    "with open(r'路径\\photo1.png','rb') as file: # 以“rb”模式打开图片\n",
    "    data = file.read()\n",
    "    with open(r'路径\\new_photo.png','wb') as newfile:\n",
    "        newfile.write(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "['Line1-abcdef ', 'Line2-abc ', 'Line4-abcd']\n['Line1-abcdef', '\\nLine2-abc \\nLine4-abcd']\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'杨英杰+...'"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "# 分割字符串\n",
    "str = \"Line1-abcdef \\nLine2-abc \\nLine4-abcd\"\n",
    "print (str.split('\\n')  )     # 以\\n为分隔符\n",
    "print (str.split(' ', 1 )) # 以空格为分隔符，分隔成两个\n",
    "\n",
    "# 合并字符串\n",
    "str2 = ('杨英杰','...')\n",
    "'+'.join(str2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#提取表格信息\n",
    "with open (r'路径\\scores.xlsx','r') as file:\n",
    "    data = file.readlines()\n",
    "    print(data)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "with open(r'路径\\poem.txt','r',encoding='utf-8')as file1:\n",
    "    data = file1.readlines()\n",
    "randline = random.randint(2,len(data)-1)\n",
    "data[randline] = '_________\\n'\n",
    "with open(r'E:路径\\NewPoem.txt','w',encoding='utf-8')as file1:\n",
    "    file1.writelines(data)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_test = ['一弦一柱思华年。\\n','只是当时已惘然。\\n']\n",
    "with open ('poem2.txt','r') as f:\n",
    "    lines = f.readlines()\n",
    "print(lines)\n",
    "with open('poem2.txt','w') as new:\n",
    "    for line in lines:\n",
    "        if line in list_test:\n",
    "            new.write('____________。\\n')\n",
    "        else:\n",
    "            new.write(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 模块(.py文件)    最高级别的程序组织单元:可存放变量、函数、类\n",
    "import test  # 导入test模块\n",
    "\n",
    "import test as t   # 导入 test 模块重命名为 t\n",
    "\n",
    "import a,b,c   #同时导入a，b，c文件\n",
    "\n",
    "from test import submodule  #从test中导入某一(变量、函数、类)\n",
    "# 导入后无需添加 'test.'的前缀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 模块test.py\n",
    "a = '我是模块中的变量a'\n",
    "\n",
    "def hi():\n",
    "    a = '我是函数里的变量a'\n",
    "    print('函数“hi”已经运行！')\n",
    "\n",
    "class Go1:  # 如果没有继承的类，class语句中可以省略括号，但定义函数的def语句括号不能省\n",
    "    a = '我是类1中的变量a'\n",
    "    @classmethod\n",
    "    def do1(cls):\n",
    "        print('函数“do1”已经运行！')\n",
    "\n",
    "class Go2:\n",
    "    a = '我是类2中的变量a'\n",
    "    def do2(self):\n",
    "        print('函数“do2”已经运行！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块\n",
    "import test  # 导入test模块\n",
    "# test 较长 可以改写为\n",
    "import test as t\n",
    "\n",
    "print(t.a)  # 使用“模块.变量”调用模块中的变量\n",
    "\n",
    "t.hi()  # 使用“模块.函数()”调用模块中的函数\n",
    "\n",
    "print(t.Go1.a)   # 使用“模块.类.变量”调用模块中的类属性\n",
    "t.Go1.do1()  # 使用“模块.类.函数()”调用模块中的类方法\n",
    "\n",
    "A = t.Go2()  # 使用“变量 = 模块.类()”实例化模块中的类\n",
    "print(A.a)  # 实例化后，不再需要“模块.”\n",
    "A.do2()  # 实例化后，不再需要“模块.”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 程序的入口\n",
    "# 【文件：xx.py】\n",
    "代码块 ①……\n",
    "#   当.py文件被当作模块调用时，跳过代码②\n",
    "if __name__ == '__main__':\n",
    "    代码块 ②……            #文件.py直接运行时不受影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 第三方库函数\n",
    "#安装模块 ： 在终端输入  pip install + 模块名\n",
    "# 查看安装模块的代码 ： cmd 模式下： import 模块名 >>> 模块名.__file__\n",
    "# 通过看源代码进行学习并非明智之举  【直接查看官方文档】or【谷歌搜索模块】\n",
    "# 查看官方文档最快方法 【先看实例遇到不懂再返回】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 比较小的模块（比如random模块）可以通过这样的方式自学，大型模块最好还是在课程上系统学习（除非你有充足的专业背景知识）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### csv模块\n",
    "\n",
    "import csv   # 无法调用.xlsx的格式\n",
    "\n",
    "with open (r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\15~17关：第5个小目标：用Python给朋友发个邮件\\test16.csv','w') as f:\n",
    "    f_csv = csv.writer(f)   # 实例化writer对象\n",
    "    f_csv.writerow(['4', '猫砂', '25', '1022', '886'])\n",
    "    f_csv.writerow(['5', '猫罐头', '18', '2234', '3121'])\n",
    "\n",
    "with open (r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\15~17关：第5个小目标：用Python给朋友发个邮件\\test16.csv') as f:\n",
    "    f_csv = csv.reader(f)  #实例化read对象\n",
    "    for row in f_csv:\n",
    "        print(row)     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']\n",
    "rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',\n",
    "        'Time':'9:36am', 'Change':-0.18, 'Volume':181800},\n",
    "        {'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',\n",
    "        'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},\n",
    "        {'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',\n",
    "        'Time':'9:36am', 'Change':-0.46, 'Volume': 935000},\n",
    "        ]\n",
    "\n",
    "with open(r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\15~17关：第5个小目标：用Python给朋友发个邮件\\stocks.csv','w') as f:\n",
    "    f_csv = csv.DictWriter(f, headers)\n",
    "    f_csv.writeheader()\n",
    "    f_csv.writerows(rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 发送邮件 smtplib模块\n",
    "# 官方文档：需要向模块输入什么；模块能做什么；模块返回的是什么；常见的报错；SMTP对象有哪些方法及如何使用；一个应用实例。\n",
    "import smtplib\n",
    "from email.mime.text import MIMEText  # 邮件内容方法\n",
    "# from email.mime.image import MIMEImage\n",
    "from email.header import Header   #  邮件头方法\n",
    "# import email 是错误的 【__init__.py控制包的导入】\n",
    "# 从email包(package)下的文件中引入方法  \n",
    "\n",
    "#编辑msg.as_string()\n",
    "text = 'Python 邮件发送测试...'\n",
    "msg = MIMEText(text,'plain')   # 正文内容\n",
    "msg['From'] = Header('y156596850@qq.com','gbk')  #发件人称呼\n",
    "msg['To'] = Header('M201871424@hust.edu.cn','utf-8')   #收件人称呼\n",
    "msg['Subject'] = Header('Python SMTP 邮件测试过程中')  #邮件标题\n",
    "# msg['Subject']代表根据MIMEText类里面的Subject的属性名取到该属性，不是每一个类都可这样访问其属性\n",
    "print(msg)\n",
    "\n",
    "# QQ邮箱授权码：jxxoyztydbpibjic\n",
    "try:\n",
    "    server = smtplib.SMTP()   #实例化一个smtplib模块里的SMTP类的对象，可以调用SMTP对象的方法和属性\n",
    "    server.connect('smtp.qq.com',25)\n",
    "    server.login('y156596850@qq.com','jxxoyztydbpibjic') \n",
    "    server.sendmail('y156596850@qq.com','M201871424@hust.edu.cn',msg.as_string()) #msg.as_string()将转化为字符串格式\n",
    "    server.quit() \n",
    "    print('邮件发送成功')\n",
    "except smtplib.SMTPException:\n",
    "    print('邮件发送失败')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成二维码\n",
    "from MyQR import myqr\n",
    "\n",
    "version, level, qr_name = myqr.run(\n",
    "    words=\"https://mp.weixin.qq.com/s/n5rB2VhMLAnq0a_p0gpznw\",          # 可以是字符串，也可以作为网址(前面要加http(s)://)\n",
    "    version=1,                              # 设置容错率为最高\n",
    "    level='H',                              # 控制纠错水平，范围是L、M、Q、H，从左到右依次升高\n",
    "    picture=r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\15~17关：第5个小目标：用Python给朋友发个邮件\\1.jpg',                           # 将二维码和图片合成\n",
    "    colorized=True,                         # 彩色二维码\n",
    "    contrast=1.0,                           #用以调节图片的对比度，1.0 表示原始图片，更小的值表示更低对比度，更大反之。默认为1.0\n",
    "    brightness=1.0,                         #用来调节图片的亮度，其余用法和取值同上\n",
    "    save_name=\"3.png\",                     # 保存文件的名字，格式可以是jpg,png,bmp,gif\n",
    "    save_dir=r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\15~17关：第5个小目标：用Python给朋友发个邮件'                    #控制位置\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 求绝对值函数\n",
    "\n",
    "def abs_value1(a):\n",
    "    y = abs(a)\n",
    "    print(y)\n",
    "\n",
    "def abs_value2():\n",
    "    a = float(input('2.请输入一个数字：'))\n",
    "    if a >= 0:\n",
    "        a = a\n",
    "    else:\n",
    "        a = -a\n",
    "    print('绝对值为：%f' % a)\n",
    "    \n",
    "def abs_value3():\n",
    "    a = float(input('3.请输入一个数字：'))\n",
    "    a = math.fabs(a)\n",
    "    print('绝对值为：%f' % a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.4 64-bit ('base': conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "interpreter": {
   "hash": "2a7e95a32014fc1ccf24626d45a98c6e7b4373277259c22f47a91d487fc3e8a5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}